﻿

namespace LxCFD
{
    public class UserMonitorBase : MonitorBase
    {
        public ScalarTypes ScalarType;
        public bool IsCellValue;
        protected void RunStatistic()
        {
            if (IsCellValue)
            {
                MultiThreads.RunCalTasks(FindCellValue);
            }
            else
            {
                MultiThreads.RunCalTasks(FindFaceValue);
            }
        }
        private void FindFaceValue(int taskIndex)
        {
            foreach (var z in AppRes.FvMesh.Zones)
            {
                foreach (var f in z.BoundThreads)
                {
                    if (f.MonitorSelector[ID])
                    {
                        var array = z.CFDFields.GetArray(ScalarType);
                        if (array == null) break;
                        CalFaceValue(z, f, array, taskIndex);
                        SetIsCurrentExistTrue(taskIndex);
                    }
                }
            }
        }
        public virtual void CalFaceValue(FvZone selectedZone, FvThread selectedThread, double[] scalarArray, int taskIndex)
        {

        }
        private void FindCellValue(int taskIndex)
        {
            foreach (var z in AppRes.FvMesh.Zones)
            {
                if (z.MonitorSelector[ID])
                {
                    var array = z.CFDFields.GetArray(ScalarType);
                    if (array == null) continue;
                    CalCellValue(z, array, taskIndex);
                    SetIsCurrentExistTrue(taskIndex);
                }
            }
        }
        public virtual void CalCellValue(FvZone selectedZone, double[] scalarArray, int taskIndex)
        {

        }
        protected void SetIsCurrentExistTrue(int taskIndex)
        {
            if (taskIndex == 0 && !IsCurrentExist) IsCurrentExist = true;
        }
    }
}

